﻿using System;
using System.IO;
using System.Windows.Forms;

namespace XuanFe.AppLaunch.Common
{
    public partial class frmError : DevExpress.XtraEditors.XtraForm
    {
        private Exception ex;
        private string path;
        private log4net.ILog logger;

        public frmError(Exception ex)
        {
            InitializeComponent();

            this.Text = "系统错误";
            this.lblSystemError.Text = "系统错误";
            this.lblSystemDesc.Text = "系统发生错误，对您造成不便表示歉意，请您将错误的信息发送给我们，我们会尽快给予技术上的支持。";
            this.lblTime.Text = "发生时间";
            this.lblUser.Text = "用户名称";
            this.lblWorkstation.Text = "计算机名";
            this.lblErrorDescriprion.Text = "错误描述";
            this.lblLocation.Text = "错误位置";

            this.btnSaveAs.Text = "另存为(&A)...";
            this.btnContinue.Text = "继续(&C)";
            this.btnExit.Text = "关闭(&X)";

            this.ex = ex;
            logger = log4net.LogManager.GetLogger("AppLogger");
        }

        private void SaveLog(string path)
        {
            StreamWriter w = new StreamWriter(path, true);
            w.WriteLine("--------------------------------------------------------------------------------");
            w.WriteLine(string.Format("发生时间：{0}", this.txtTime.Text));
            w.WriteLine(string.Format("用户名称：{0}", this.txtUser.Text));
            w.WriteLine(string.Format("计算机名：{0}", this.txtWorkstation.Text));
            w.WriteLine(string.Format("操作系统：{0}", Global.Instance.GetOSVersionString()));
            w.WriteLine(string.Format("程序版本：{0}", Application.ProductVersion));
            w.WriteLine(string.Format("错误描述：{0}", this.txtErrorDescriprion.Text));
            w.WriteLine(string.Format("错误位置：{0}", this.txtLocation.Text));
            w.Flush();
            w.Close();
        }

        private void frmError_Load(object sender, EventArgs e)
        {
            this.btnExit.Visible = Application.AllowQuit;

            this.txtLocation.Text = this.ex.StackTrace.Trim();
            this.txtTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            this.txtUser.Text = "UserInfo.UserId";
            this.txtWorkstation.Text = System.Net.Dns.GetHostName();
            this.txtErrorDescriprion.Text = this.ex.Message.ToString();

            this.path = string.Format("{0}\\ErrorLogs", Path.GetDirectoryName(Application.ExecutablePath));
            Directory.CreateDirectory(this.path);

            if (this.logger.IsErrorEnabled)
            {
                this.logger.Error("系统错误", this.ex);
            }
        }

        private void btnContinue_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnSaveAs_Click(object sender, EventArgs e)
        {
            dlgSaveFile.FileName = string.Format("{0}_{1}", this.ex.Source, DateTime.Now.ToString("yyyyMMddHHmmss"));
            dlgSaveFile.InitialDirectory = this.path;
            if (dlgSaveFile.ShowDialog() == DialogResult.OK)
            {
                this.SaveLog(dlgSaveFile.FileName);
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
